//
//  NBJSuggestionPinAnnotationView.m
//  nbj
//
//  Created by Francois Proulx on 09-10-04.
//  Copyright 2009 Toujours Saoul. All rights reserved.
//

#import "NBJSuggestionPinAnnotationView.h"

#import "NBJSuggestionAnnotation.h"

// Majority of this code is straight from Apple documentation :
// http://developer.apple.com/IPhone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedFeatures/AdvancedFeatures.html#//apple_ref/doc/uid/TP40007072-CH14-SW40

@implementation NBJSuggestionPinAnnotationView

@synthesize map;

- (id)initWithAnnotation:(id <MKAnnotation>)annotation
{
	self = [super initWithAnnotation:annotation reuseIdentifier:@"NBJSuggestionPinAnnotationView"];
	if (self) {
		self.image = [UIImage imageNamed:@"green-pin.png"];
		self.centerOffset = CGPointMake(11, -19);
		self.calloutOffset = CGPointMake(-10, -2);
		self.canShowCallout = YES;
		self.multipleTouchEnabled = NO;
		map = nil;

		UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[addButton setBackgroundImage:[UIImage imageNamed:@"bar_suggestion_button.png"] forState:UIControlStateNormal];
		[addButton sizeToFit];
		self.leftCalloutAccessoryView = addButton;
	}
	
	return self;
}

#pragma mark -
#pragma mark UITouch handling

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	// The view is configured for single touches only.
	UITouch* aTouch = [touches anyObject];
	startLocation = [aTouch locationInView:[self superview]];
	originalCenter = self.center;

	[super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch* aTouch = [touches anyObject];
	CGPoint newLocation = [aTouch locationInView:[self superview]];
	CGPoint newCenter;

	// If the user's finger moved more than 5 pixels, begin the drag.
	if ( (abs(newLocation.x - startLocation.x) > 5.0) || (abs(newLocation.y - startLocation.y) > 5.0) ) {
		isMoving = YES;
	}

	// If dragging has begun, adjust the position of the view.
	if (isMoving) {
		newCenter.x = originalCenter.x + (newLocation.x - startLocation.x);
		newCenter.y = originalCenter.y + (newLocation.y - startLocation.y);
		self.center = newCenter;
	} else { 
		// Let the parent class handle it.
		[super touchesMoved:touches withEvent:event];
	}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	if (isMoving) {
		// Update the map coordinate to reflect the new position.
		CGPoint newCenter = self.center;
		NBJSuggestionAnnotation *theAnnotation = self.annotation;
		CLLocationCoordinate2D newCoordinate = [map convertPoint:newCenter toCoordinateFromView:self.superview];
		[theAnnotation changeCoordinate:newCoordinate];

		// Clean up the state information.
		startLocation = CGPointZero;
		originalCenter = CGPointZero;
		isMoving = NO;
	} else {
		[super touchesEnded:touches withEvent:event];
	}
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
	if (isMoving) {
		// Move the view back to its starting point.
		self.center = originalCenter;

		// Clean up the state information.
		startLocation = CGPointZero;
		originalCenter = CGPointZero;
		isMoving = NO;
	} else {
		[super touchesCancelled:touches withEvent:event];
	}
}

@end
